package edu.pku.dao;

import java.sql.SQLException;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

import edu.pku.data.Company;
import edu.pku.util.MySqlMapClient;

public class CompanyDao {
	
	public void addCompany(Company company) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		try{
			sqlMap.startTransaction();
			sqlMap.startBatch();
			sqlMap.insert("insert_company",company);
			sqlMap.insert("create_buy_form", company.getName());
			sqlMap.insert("create_user_role", company.getName());
			sqlMap.insert("create_privilege", company.getName());
			sqlMap.insert("create_company_user", company.getName());		
			sqlMap.insert("create_depository", company.getName());
			sqlMap.insert("create_depository_shell", company.getName());
			sqlMap.insert("create_friend_company", company.getName());
			sqlMap.insert("create_product_category", company.getName());
			sqlMap.insert("create_product", company.getName());
			sqlMap.insert("create_product_store", company.getName());
			sqlMap.insert("create_sell_form", company.getName());
			sqlMap.insert("create_out_replayprice", company.getName());
			sqlMap.insert("create_out_askprice", company.getName());
			sqlMap.insert("create_in_replayprice", company.getName());
			sqlMap.insert("create_in_askprice", company.getName());
			sqlMap.insert("create_import_form", company.getName());
			sqlMap.insert("create_export_form", company.getName());
			sqlMap.executeBatch();
			sqlMap.commitTransaction();
		}catch(Exception e)
		{
		}finally{
			sqlMap.endTransaction();
		}
	}
	
	public boolean isNameExist(String name) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		List<String> list = sqlMap.queryForList("get_company_name");
		if(list.contains(name))
			return true;
		return false;
	}
	
	public Company getCompanyByName(String name) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		Company company = (Company)sqlMap.queryForObject("get_company_by_name",name);
		return company;
	}
	
}
